package de.wefunkdroid.core;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import de.wefunkdroid.core.data.Show;
import de.wefunkdroid.core.data.Track;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class PlayerActivity extends Activity {

	Streamplayer pl;
	Show s;
	int curTrack = 0;

	public PlayerActivity() {
	}

	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		String shownum = "";
		super.onCreate(savedInstanceState);
		Intent i = getIntent();
		setContentView(R.layout.playerlayout);
		if (i != null && i.hasExtra("Show") && i.hasExtra("ShowNum")) {
			shownum = i.getStringExtra("ShowNum");
			if (i.hasExtra("TrackNum")) {
				curTrack = Integer.valueOf(i.getStringExtra("TrackNum"));
			}
		}
		String date = "";
		String str = "";
		pl = new Streamplayer();
		try {
			date = Parser.getDateFromNum(shownum);
			URLConnection con = (new URL((new StringBuilder(
					"http://www.wefunkradio.com/show/")).append(date)
					.toString())).openConnection();
			char buffer[] = new char[8192];
			BufferedInputStream bis = new BufferedInputStream(con
					.getInputStream());
			InputStreamReader isread = new InputStreamReader(bis);
			BufferedReader bread = new BufferedReader(isread);
			for (int read = 0; (read = bread.read(buffer, 0, 8192)) != -1;)
				str = (new StringBuilder(String.valueOf(str))).append(
						new String(buffer, 0, read)).toString();

			bread.close();
			isread.close();
			bis.close();
		} catch (Exception e) {
			Log.e("WeFunkDroidActivity", "Error", e);
		}
		s = Parser.parse(str, date);
		ListView listeview = (ListView) findViewById(R.id.listView1);
		String artrck[] = new String[s.getTracks().size()];
		for (int k = 0; k < artrck.length; k++)
			artrck[k] = ((Track) s.getTracks().get(k)).toString();

		listeview.setAdapter(new ArrayAdapter<String>(this, R.layout.listitem,
				artrck));
		listeview.setTextFilterEnabled(true);
		listeview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				curTrack = position;
				pl.seek(Integer.valueOf(
						((Track) s.getTracks().get(position)).getMsPos())
						.intValue());
			}

		});
		pl.prepareNewMedia(Parser.getALink(s));
		System.out.println(Parser.getALink(s));
		pl.startPlayer();
		if (curTrack > 0)
			pl.seek(Integer.valueOf(s.getTracks().get(curTrack).getMsPos()));
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case 85: // 'U'
			pl.toggleplay();
			return true;

		case 87: // 'W'
			back();
			return true;

		case 88: // 'X'
			skip();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void skip() {
		if (curTrack >= s.getTracks().size()) {
			return;
		} else {
			curTrack++;
			pl.seek(Integer.valueOf(
					((Track) s.getTracks().get(curTrack)).getMsPos())
					.intValue());
			return;
		}
	}

	private void back() {
		if (curTrack < 0) {
			return;
		} else {
			curTrack--;
			pl.seek(Integer.valueOf(
					((Track) s.getTracks().get(curTrack)).getMsPos())
					.intValue());
			return;
		}
	}

	protected void onDestroy() {
		WefunkStatics.getInstance(this).save(s);
		pl.killPlayer();
		super.onDestroy();
	}

	public void guiBack(View v) {
		back();
	}

	public void guiSkip(View v) {
		skip();
	}

	public void guiTogglePlay(View v) {
		pl.toggleplay();
	}

}
